/**
 * Created by Administrator on 2017/7/20.
 */
(function() {
    "use strict";
    var app = angular.module("angularApp");
    app.controller('styleTypeCtrl', function ($scope, $http,$uibModal,fac) {
        $scope.search = {};
        var curEle;
        $scope.showTab = function(e){
            curEle = e;
            e.pageModel = e.pageModel||{};
            if(!e.pageModel.data){
                $scope.find(1);
            }
        }
        $scope.find = function(pageNo){
            $.extend($scope.search,{currentPage:pageNo||curEle.pageModel.currentPage||1,pageSize:curEle.pageModel.pageSize||20000},{element:curEle.id});
            fac.getPageResult("../styleType/list",$scope.search,function(data){
                curEle.pageModel = data;
            });
        };
        $scope.$on("refreshStyleType",function(){
            $scope.find();
            $rootScope.elementEnum.forEach(function(n) {
                    if (n != curEle) {
                        n.pageModel && delete n.pageModel.data
                    }} )
        })
        //删除
        $scope.del = function(item){
            confirm("确认删除该样式分类吗？",function(){
                $http.get("../styleType/del?styleTypeId="+item.id).success(function(resp){
                    if(resp.code === 0){
                        $scope.find();
                    }else{
                        alert(resp.msg);
                    }
                })
            });
        }

        //添加与保存app
        $scope.showEditModal = function(item){
            var modal = $uibModal.open({
                animation: false,
                size:'',
                templateUrl: 'view/styleType/styleType.modal.html',
                controller: 'styleTypeModalCtrl'
                ,resolve: {item: angular.extend({},item||{element:curEle.id})}
            });
            modal.result.then(function () {
                $scope.find();
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        };
    });

    app.controller('styleTypeModalCtrl', function($scope,$http,$uibModalInstance,fac,item) {
        $scope.item = item;

        $scope.save = function (form) {
            form.$setSubmitted(true);
            if (!form.$valid) {
                return;
            }
            $http.post("../styleType/save", item).success(function (data, status, headers, config) {
                if (data.code === 0) {
                    $uibModalInstance.close();
                    msg("保存成功!");
                } else {
                    alert(data.msg);
                }
            })
        }
    });
})();